﻿@using Ext.Net
@using Ext.Net.MVC
@model SDSystem.Models.TypeOfBusiness
           
@{
    ViewBag.Title = "Lookup";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Header
{
    <script type="text/javascript">
        function lookup() {
            if (App.DataGridPanel.getSelectionModel().getSelection()[0] == null) {
                alert('No business selected');
                return;
            }
            var value1 = App.DataGridPanel.getSelectionModel().getSelection()[0].get('BusinessName');
            var id = App.DataGridPanel.getSelectionModel().getSelection()[0].get('BusinessId');
            var value2 = App.DataGridPanel.getSelectionModel().getSelection()[0].get('SICcode');
            alert('Business Name : ' + value1 +', SIC code : ' + value2);
            parent.Ext.WindowMgr.getActive().hide();
            parent.Ext.getCmp('@Request.QueryString["text1"]').setValue(value1);
            parent.Ext.getCmp('@Request.QueryString["id"]').setValue(id);
            parent.Ext.getCmp('@Request.QueryString["text2"]').setValue(value2);
        }
    </script>
}
@Html.X().FieldSet().ID("SearchForm").Title("Search Business").Collapsible(true).Layout(LayoutType.Form).Items(
    Html.X().FieldContainer().Layout(LayoutType.HBox).Items(
                Html.X().TextField().ID("txtBusinessName").FieldLabel("Business Name").Flex(4),
                Html.X().TextField().ID("txtSicCode").FieldLabel("SIC Code").Flex(1).Margins("0 0 0 10")
        ),

    Html.X().FieldContainer().Layout(LayoutType.HBox).Items(
        Html.X().Button().Text("Search").Icon(Icon.Find).DirectEvents(
            de =>
            {
                de.Click.Url = Url.Action("Search");
                de.Click.FormID = "SearchForm";
            }),
            Html.X().Button().Text("None").Margins("0 0 0 10").DirectEvents(
                de =>
                {
                    de.Click.Url = Url.Action("ClearSearch");
                    de.Click.FormID = "SearchForm";
                }),
        Html.X().Button().Text("Clear").Margins("0 0 0 10").Listeners(ls =>
                ls.Click.Handler = "App.txtPostCode.setValue('');App.txtStreet.setValue('');App.txtTown.setValue('');"
        )
        )

     )
@* GridPanel *@
@(Html.X().GridPanel().ID("DataGridPanel")
        .Title("Select Business")
        .Icon(Icon.Table)
        .Frame(true)
        .Store(Html.X().Store().ID("BusinessStore")
                .Model(
                    Html.X().Model().IDProperty("BusinessId")
                    .Fields(
                        new ModelField("BusinessId", ModelFieldType.Int),
                        new ModelField("BusinessName", ModelFieldType.String),
                        new ModelField("SICcode", ModelFieldType.Int)
                    )
                )
                .RemoteSort(true)
                .RemotePaging(true)
                .PageSize(2)
                .Proxy(
                    Html.X().AjaxProxy()
                    .Url(Url.Action("BindingStore"))
                    .Reader(Html.X().JsonReader().Root("data"))
                )
        )
        .ColumnModel(
            Html.X().Column().Text("Business Name").DataIndex("BusinessName").Flex(3),
            Html.X().Column().Text("SIC Code").DataIndex("SICcode").Flex(1)
        )
        .BottomBar(Html.X().PagingToolbar())
)
<div style="margin-top: 10px">
    @Html.X().ButtonGroup().Items(
        Html.X().Button().Text("Select").Icon(Icon.Accept).Listeners(li => li.Click.Handler = "lookup();" ),
        Html.X().Button().Text("Close").Icon(Icon.Decline).Listeners(ls => ls.Click.Handler = "parent.Ext.WindowMgr.getActive().hide();")
    )
</div>
